package com.erp.module.miMa

import android.annotation.SuppressLint
import androidx.appcompat.app.AlertDialog
import android.content.Intent
import android.util.Log
import android.view.View
import com.erp.Interface.OnUserDataInterface
import com.erp.activity.BaseActivity
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_mima.*
import kotlinx.android.synthetic.main.header_knoya.*
import org.json.JSONException
import org.json.JSONObject
import java.util.*



/**
 * Created by Administrator on 2018/1/24/024.
 * 作者：刘洋
 */

class MiMaActivity : BaseActivity(), View.OnClickListener {
    lateinit var updatedialog: AlertDialog
    private var recLen = 300
    private var ischongfa = false
    private val a = "MiMaActivity"
    override fun getLayoutId(): Int {
        activityname = a
        return com.erp.R.layout.activity_mima
    }

    override fun initCreate() {

        iv_header_left.visibility = View.VISIBLE
        tv_header_title.text = "找回密码"
        TvChongfa.text = "获取验证码"
        TvChongfa.setOnClickListener(this)
        BtQueding.setOnClickListener(this)
    }

    var timer = Timer()
    var task: TimerTask = object : TimerTask() {
        @SuppressLint("SetTextI18n")
        override fun run() {
            runOnUiThread {
                recLen--
                TvChongfa.text = recLen.toString() + "秒后重新发送"
                if (recLen < 0) {
                    timer.cancel()
                    ischongfa = true
                    TvChongfa.setText("获取验证码")
                }
            }
        }
    }


    override fun onClick(v: View) {
        when (v) {
            TvChongfa -> {
                if (ischongfa) {
                    return
                }
                val s = EtPhone.text.toString()
                if (!Myutil.isTel(s)) {
                    TvTishi.text = "请输入正确的手机号码"
                    return
                }
                updatedialog = Myutil.showupdatedialog(this@MiMaActivity, "正在请求验证码", false)
                Httputil.getYanZheng(this@MiMaActivity, s, object : OnUserDataInterface {
                    override fun onUserData(data: String) {
                        Log.d("TAAAG", "data=" + Myutil.decodeUnicode(data))
//                        "{\"msg\":\"验证码发送成功\"}"

                            try {

                                val json = JSONObject(data)
                                val it = json.keys()
                                for(key in it){
                                    Myutil.MyTost(Myutil.decodeUnicode(JsonUtil.getvolue(json,key)))
                                }

                            }catch (e : JSONException){
                                e.printStackTrace()
                                Myutil.MyTost(Myutil.decodeUnicode(data))
                            }
                        timer.schedule(task, 1000, 1000);
                        ischongfa = false
                        updatedialog.dismiss()
                    }

                    override fun onNot(throwable: String) {
                        Myutil.MyTost(Myutil.decodeUnicode(throwable))
                        Log.d("TAAAG", "th=$throwable")
                        setcuowu(throwable)
                        updatedialog.dismiss()
                    }
                })
            }
            BtQueding -> {
                val phone = EtPhone.text.toString()
                val yanzheng = EtYanzheng.text.toString()
                if (phone.length != 11) {
                    Myutil.MyTost("请输入正确的手机号码")
                    return
                }
                if (yanzheng == "") {
                    Myutil.MyTost("请输入验证码")
                    return
                }
                updatedialog = Myutil.showupdatedialog(this@MiMaActivity, "正在验证", false)
                Httputil.YanZheng(this@MiMaActivity, phone, yanzheng, object : OnUserDataInterface {
                    override fun onUserData(data: String) {
                        Log.d("TAAAG", "data=$data")
                        chenggong(data)
                        updatedialog.dismiss()
                    }

                    override fun onNot(throwable: String) {
                        Log.d("TAAAG", "throwable=$throwable")
                        setcuowu(throwable)
                        updatedialog.dismiss()
                    }
                })
            }
        }
    }

    private fun setcuowu(throwable: String) {
        val ss = throwable.split("\"".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        if (ss.size >= 2) {
            TvTishi.text = Myutil.decodeUnicode(ss[1])
        }
    }

    private fun chenggong(data: String) {
        var password = ""
        try {
            val jo = JSONObject(data)
            password = JsonUtil.getvolue(jo, "password")
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        val intent = Intent(this@MiMaActivity, MiMaFanHuiKActivity::class.java)
        intent.putExtra("password", password)
        startActivity(intent)
        finish()
    }

}
